//
//  XDSelectCourseItemCell.m
//  XDEducation
//
//  Created by apple on 2017/4/14.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDSelectCourseItemCell.h"

@interface XDSelectCourseItemCell()
{

    NSArray *_itemArr;
    UIButton *_selectedButton;
}
@end

@implementation XDSelectCourseItemCell

+(instancetype)cellWithTableView:(UITableView *)tableView itemDataArr:(NSArray *)itemArr cellId:(NSString *)cellId{

    XDSelectCourseItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        
        cell = [[XDSelectCourseItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId itemDataArr:(NSMutableArray *)itemArr];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier itemDataArr:(NSArray *)itemArr{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _itemArr = itemArr;
        
        CGFloat itemW = 100 * ScaleWidth;
        CGFloat itemH = 35 * ScaleHeight;
        
        
        CGFloat padding = (ScreenW - 3 * itemW)/4;
        CGFloat paddingY = 20;
        
        CGFloat itemX = padding;
        CGFloat itemY = paddingY;
        
        int tempI = 0;
        int tempJ = 0;
        for (int i = 0; i < itemArr.count; i ++) {
            
            NSString *itemName = itemArr[i][@"name"];
            
            
            BOOL isMultipleOfFour = !(i % 3);
            
            if (isMultipleOfFour && i != 0) {
                tempJ ++;
                tempI = 0;
                itemY = paddingY + (itemH + paddingY) * tempJ;
            }
            
            
            UIButton *button = [[UIButton alloc]init];
            button.frame =  CGRectMake(itemX + tempI * (padding + itemW), itemY , itemW, itemH);
            
            
            [button setTitle:itemName forState:UIControlStateNormal];
            
            [button setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            
            [button setBackgroundImage:[UIImage imageNamed:@"list_btn_blue_default"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"list_btn_blue_selected"] forState:UIControlStateSelected];
            
            //button.titleLabel.font = iPhone5?[UIFont systemFontOfSize:10]:[UIFont systemFontOfSize:12];
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            button.titleLabel.textAlignment = NSTextAlignmentCenter;
            [self.contentView addSubview:button];
            
            tempI ++;
            
            button.tag = i;
            
            
            
            [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            
        }

        
    }
    return self;
}

-(void)buttonClicked:(UIButton *)button{

    button.selected = !button.selected;
    if (_selectedButton.selected) {
        
        _selectedButton.selected = !_selectedButton.selected;
        
    }
    _selectedButton = button;
    
    if (self.itemClickdedBlock) {
        self.itemClickdedBlock(button.tag,button.selected);
    }
}

@end
